<?php
include ("../lib/BLL/TblKhoahocBLL.php");

$thongbao="";
$khoahocBLL=new TblKhoahocBLL();
if(isset($_GET['act']))
     $act =  $_GET['act'];
else $act = '';
if(isset($_GET['id']))
     $id =  $_GET['id'];
else $id = '';
switch($act){
    case 'add': 
        if(isset($_POST['luu'])){     
            $nam=$_POST['namkhoahoc'];
            $mota=$_POST['motakhoahoc'];
            $donvidt=$_POST['donvi'];
            $canbo=$_POST['canbo'];
            
            $khoahoc= new TblKhoahoc();
            $khoahoc->setKH($nam,$mota,$donvidt,$canbo);
            $kq=$khoahocBLL->insertKhoahoc($khoahoc);           
            if($kq)
            {
                $thongbao="Đã thêm thành công!";
            }else{
                $thongbao="SAI rồi nhé";
            }    
            
        }          
        $smarty->assign('thongbao',$thongbao);
        $dscb=$khoahocBLL->getlistcb();
        $smarty->assign('ds_cb',$dscb);
        $dsdv=$khoahocBLL->getlistdv();
        $smarty->assign('ds_dv',$dsdv); 
        $smarty->display('nhom3/tblkhoahoc/Add_tblkhoahoc.tpl');
        break;
    case 'edit':
        if(isset($_POST['sua'])){     
            $nam=$_POST['namkhoahoc'];
            $mota=$_POST['motakhoahoc'];
            $donvidt=$_POST['donvi'];
            $canbo=$_POST['canbo'];

            $khoahoc= new TblKhoahoc();
            $khoahoc->setKhoahoc($id,$nam,$mota,$donvidt,$canbo);
            $kq=$khoahocBLL->updateKhoahoc($khoahoc);         
            if($kq)
            {
                $thongbao="Đã sửa thành công!";
                $_SESSION['status']=1; 
                //tạo session de danh dau la da sua thanh cong
                header("Location: ?muc=nhom3&modul=khoahoc");
            }else{
                $thongbao="SAI rồi nhé";
            }    
            
        }          
        $smarty->assign('thongbao',$thongbao);
        
        $dscb=$khoahocBLL->getlistcb();
        $smarty->assign('ds_cb',$dscb);
        $dsdv=$khoahocBLL->getlistdv();
        $smarty->assign('ds_dv',$dsdv);
        
        $result=$khoahocBLL->getKhoahocinfo($id);  
                            
        $smarty->assign('kq',$result);
        
        $smarty->display('nhom3/tblkhoahoc/Edit_tblkhoahoc.tpl');
        break;
    case 'delete': 
        $khoahocBLL->deletekhoahoc($id);

        $ds=$khoahocBLL->getListkhoahocFull();  
        $smarty->assign('ds_khoahoc',$ds); 
        header("Location:index.php?muc=nhom3&modul=khoahoc") ;
        break; 
    case '':               
        $tb='';
        $i = 0;
        if(isset($_SESSION['status']))
        {                  
           if($_SESSION['status'] == 1) 
           { 
               $tb =  'Sửa thành công';
               $_SESSION['status'] =  0;
               
           }
          else  if($_SESSION['status'] ==2)
           {
               $tb = ' Xóa thành công';
               $_SESSION['status'] =  0;
           }
            
          else  if($_SESSION['status'] ==0)
            $tb='';
        }
        else $tb= '';               
        $smarty->assign('tb',$tb);
       
        if(isset($_POST['search'])) {
             $nam = $_POST['namkh'];
             $can_bo = $_POST['cbkh'];
             $i = 1;
        }   
        else {$nam = ''; $can_bo ='';} 
        if($i==1){
            $ds_nam=$khoahocBLL->getKhoahocinfoSerch($nam,$can_bo);  
            $smarty->assign('ds_khoahoc',$ds_nam);    
        }
        else {
            $ds=$khoahocBLL->getListkhoahocFull();  
            $smarty->assign('ds_khoahoc',$ds);     
        }
        $smarty->display('nhom3/tblkhoahoc/List_tblkhoahoc.tpl') ;
        break;
}  

?>